#include "client.h"
void count_files(const char *dir_path, st *pSt)
{
    DIR *dir;
    struct dirent *entry;
    int count = 0;

    dir = opendir(dir_path);
    if (dir == NULL)
    {
        perror("opendir");
        return;
    }
    while ((entry = readdir(dir)) != NULL)
    {
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
        {
            continue;
        }

        ImgEnqueue(pSt, entry->d_name, count);
        count++;
    }
    closedir(dir);
}

int ImgEnqueue(st *pImgQueue, char *filename, int ID)
{
    int len = strlen(filename) + 1;
    up_img *pUp_img = (up_img *)malloc(sizeof(up_img));
    if (NULL == pUp_img)
    {
        perror("malloc");
        return -1;
    }

    strcpy(pUp_img->buf, filename);
    pUp_img->ID = ID;
    printf("%d\n", pImgQueue->size);
    if (pImgQueue->size == 0)
    {

        pImgQueue->pFront = pUp_img;
        pImgQueue->pRear = pUp_img;
    }
    else
    {

        pImgQueue->pRear->pNext = pUp_img;
        pImgQueue->pRear = pUp_img;
    }
    ++pImgQueue->size;

    return 0;
}

int ImgDequeue(st *pImgQueue)
{
    up_img *pCur = pImgQueue->pFront;
    pImgQueue->pFront = pCur->pNext;
    free(pCur);
    --pImgQueue->size;
    return 0;
}
st *pImgQueueInit()
{
    st *pImgQueue = (st *)malloc(sizeof(st));
    pImgQueue->pFront = NULL;
    pImgQueue->pRear = NULL;
    pImgQueue->size = 0;
    return pImgQueue;
}
void uploadingTask(int net_fd)
{

    puts("up");
    st *imgs = pImgQueueInit();
    size_t i = 0;
    char buf[1024];
    sprintf(buf, "/home/leao/work/sdSys/project/distributed-image-processing/dis_img_pro/client/srcImgs");
    count_files(buf, imgs);
    puts("coutfile ok");
    printf("imgs=%d\n", imgs->size);
    send(net_fd, TASK_UP, 1024, 0);
    recv(net_fd, buf, 1024, 0);
    if (0 == strcmp(buf, TASK_ON))
    {
        puts(buf);

        // 在线校验
        int cout = imgs->size;
        for (int i = 0; i < cout; i++)
        {
            bzero(buf, 1024);
            puts(imgs->pFront->buf);
            send(net_fd, TASK_ON, 1024, 0);
            transFile(net_fd, imgs->pFront->buf);
            printf("i=%d\n", i);
            puts(imgs->pFront->buf);
            printf("now_imgs=%d\n", imgs->size);
            ImgDequeue(imgs);
            bzero(buf, 1024);
            printf("now_imgs=%d\n", imgs->size);
            while (1)
            {
                recv(net_fd, buf, 1024, 0);
                if (0 == strcmp(buf, TASK_ON)) // 多次任务递交校验
                {
                    break;
                }
            }
        }
        send(net_fd, TASK_SND_OVER, 1024, 0);
    }
    else
    {
        puts("服务器转发异常");
    }
    puts("任务分发完成开始执行");
}
void localTasks(int netFd){
    puts("local Task");
   char buf[1024] = {0};
   while (1)
   {
    puts("1");
      send(netFd, TASK_ON, 1024, 0);
        puts("2");
      recv(netFd, buf, 1024, 0);
       puts("3");
      if (0 == strcmp(buf, TASK_ON))
      {
        puts(buf);
         recvTask(netFd);
      }
      else if (0 == strcmp(buf, TASK_SND_OVER))
      {
         puts(buf);
         break;
      }
   }
}
// {
//     puts("localTask");
//     //
//     send(net_fd, TASK_ON, 1024, 0);

//     char buf[1024];
//     puts(buf);
//     while (1)
//     {
//         recv(net_fd, buf, 1024, 0);
//         if (0 == strcmp(buf, TASK_DOWN))
//         {
//             puts("接受在线任务");
//             recvTask(net_fd);
//             send(net_fd, TASK_ON, 1024, 0); // 多次任务接收；
//         }
//         else if (0 == strcmp(buf, TASK_SND_OVER))
//         {
//             break;
//         }
//     }
//     /////////任务队列接受完成；开始任务
//     st *online_task;
//     sprintf(buf, "/home/leao/work/sdSys/project/distributed-image-processing/dis_img_pro/client/online_task");
//     count_files(buf, online_task);
//     for (size_t j = 0; j < online_task->size; j++)
//     {
//         bzero(buf, 1024);
//         bzero(buf, 1024);
//         sprintf(buf, "conda activate py39");
//         system(buf);
//         sprintf(buf, "/home/leao/work/sdSys/ultralytics/ yolo task=detect mode=predict model=yolov8n.pt source=/home/leao/work/sdSys/project/distributed-image-processing/dis_img_pro/client/online_task/");
//         system(buf);
//         ImgDequeue(online_task);
//     }
// }
// void uptsk(int net_fd, int taskQueue)
// {
//     puts("uptsk");

//     st *imgs = pImgQueueInit();
//     size_t i = 0;
//     char buf[1024];
//     sprintf(buf, "/home/leao/work/sdSys/project/distributed-image-processing/dis_img_pro/client/srcImgs");

//     count_files(buf, imgs);
//     puts("coutfile ok");
//     printf("imgs=%d\n", imgs->size);
//     // char tasks[16];
//     int online = 0;
//     printf("tasksQ=%d\n", taskQueue);
//     if (taskQueue > 1)
//     {

//         send(net_fd, TASK_NEW, 1024, 0);
//         recv(net_fd, buf, 1024, 0);

//         if (0 == strcmp(buf, TASK_ON))
//         {
//             sprintf(buf, "%d", imgs->size / taskQueue);
//             printf("task_num-->%d\n", imgs->size / taskQueue);
//             puts(buf);
//             // 在线校验
//             send(net_fd, buf, 4, 0);
//             puts(buf);
//             recv(net_fd, buf, 4, 0);
//             puts(buf);
//             online = atoi(buf);
//             printf("online =%d\n", online);
//             recv(net_fd, buf, 1024, 0);
//             puts(buf);
//             if (0 == strcmp(TASK_ON, buf))
//             {
//                 send(net_fd, TASK_UP, 1024, 0);
//                 puts("123123");
//                 for (; i < imgs->size; i++)
//                 {
//                     puts("发送");
//                     bzero(buf, 1024);
//                     transFile(net_fd, imgs->pFront->buf);
//                     usleep(10000);
//                     ImgDequeue(imgs);
//                     bzero(buf, 1024);
//                     recv(net_fd, buf, 1024, 0);
//                     if (0 == strcmp(buf, TASK_ON)) // 多次任务递交校验
//                     {
//                         break;
//                     }
//                 }
//                 send(net_fd, TASK_SND_OVER, 1024, 0);
//             }
//             else
//             {
//                 puts("服务器转发异常");
//             }
//         }
//     }
//     puts("任务分发完成");
// }